package com.atlin.project.controller;

import com.atlin.project.annotation.AuthCheck;
import com.atlin.project.common.BaseResponse;
import com.atlin.project.common.DeleteRequest;
import com.atlin.project.common.ErrorCode;
import com.atlin.project.common.ResultUtils;
import com.atlin.project.contant.UserConstant;
import com.atlin.project.exception.BusinessException;
import com.atlin.project.exception.ThrowUtils;
import com.atlin.project.model.domain.SysUser;
import com.atlin.project.model.domain.className_C;
import com.atlin.project.model.dto.className_L.className_CAddRequest;
import com.atlin.project.model.dto.className_L.className_CQueryRequest;
import com.atlin.project.model.dto.className_L.className_CUpdateRequest;
import com.atlin.project.model.vo.className_CVO;
import com.atlin.project.service.SysUserService;
import com.atlin.project.service.className_CService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.gson.Gson;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 模版接口
 *
 * @author atlin
 */
@RestController
@RequestMapping("/className_L")
public class className_CController {

    @Resource
    private className_CService className_LService;

    @Resource
    private SysUserService sysUserService;

    private final static Gson GSON = new Gson();

    // region 增删改查

    /**
     * 创建
     *
     * @param className_LAddRequest
     * @param request
     * @return
     */
    @PostMapping("/add")
    public BaseResponse<Long> addclassName_C(@RequestBody className_CAddRequest className_LAddRequest, HttpServletRequest request) {
        if (className_LAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        className_C className_L = new className_C();
        BeanUtils.copyProperties(className_LAddRequest, className_L);

        className_LService.valid(className_L, true);
        SysUser loginUser = sysUserService.getLoginUser(request);

        // todo

        boolean result = className_LService.save(className_L);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        long newclassName_CId = className_L.getId();
        return ResultUtils.success(newclassName_CId);
    }

    /**
     * 删除
     *
     * @param deleteRequest
     * @param request
     * @return
     */
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteclassName_C(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        SysUser user = sysUserService.getLoginUser(request);
        long id = deleteRequest.getId();
        // 判断是否存在
        className_C oldclassName_L = className_LService.getById(id);
        ThrowUtils.throwIf(oldclassName_L == null, ErrorCode.NULL_ERROR);

        // 仅本人或管理员可删除
        if (!oldclassName_L.getUserId().equals(user.getId()) && !sysUserService.isAdmin(request)) {
            throw new BusinessException(ErrorCode.NO_AUTH_ERROR);
        }
        boolean b = className_LService.removeById(id);
        return ResultUtils.success(b);
    }

    /**
     * 更新（仅管理员）
     *
     * @param className_LUpdateRequest
     * @return
     */
    @PostMapping("/update")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> updateclassName_C(@RequestBody className_CUpdateRequest className_LUpdateRequest) {
        if (className_LUpdateRequest == null || className_LUpdateRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        className_C className_L = new className_C();
        BeanUtils.copyProperties(className_LUpdateRequest, className_L);

        // 参数校验
        className_LService.valid(className_L, false);
        long id = className_LUpdateRequest.getId();
        // 判断是否存在
        className_C oldclassName_L = className_LService.getById(id);
        ThrowUtils.throwIf(oldclassName_L == null, ErrorCode.NULL_ERROR);
        // todo other operation

        boolean result = className_LService.updateById(className_L);
        return ResultUtils.success(result);
    }

    /**
     * 根据 id 获取
     *
     * @param id
     * @return
     */
    @GetMapping("/get")
    public BaseResponse<className_C> getclassName_CById(long id, HttpServletRequest request) {
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        className_C className_L = className_LService.getById(id);
        if (className_L == null) {
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        SysUser loginUser = sysUserService.getLoginUser(request);
        // 不是本人或管理员，不能直接获取所有信息
        if (!className_L.getUserId().equals(loginUser.getId()) && !sysUserService.isAdmin(loginUser)) {
            throw new BusinessException(ErrorCode.NO_AUTH_ERROR);
        }
        return ResultUtils.success(className_L);
    }

    /**
     * 根据 id 获取
     *
     * @param id
     * @return
     */
    @GetMapping("/get/vo")
    public BaseResponse<className_CVO> getclassName_CVOById(long id, HttpServletRequest request) {
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        className_C className_L = className_LService.getById(id);
        if (className_L == null) {
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }

        className_CVO className_LVO = className_CVO.objToVo(className_L);
        return ResultUtils.success(className_LVO);
    }

    /**
     * 分页获取列表（封装类）
     *
     * @param className_LQueryRequest 查询参数
     * @param request              查询请求
     * @return 分页封装类
     */
    @PostMapping("/list/page/vo")
    public BaseResponse<Page<className_CVO>> listclassName_CVOByPage(@RequestBody className_CQueryRequest className_LQueryRequest,
                                                               HttpServletRequest request) {
        long current = className_LQueryRequest.getCurrent();
        long size = className_LQueryRequest.getPageSize();
        // 限制爬虫
        ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
        Page<className_C> className_LPage = className_LService.page(new Page<>(current, size),
                className_LService.getQueryWrapper(className_LQueryRequest));

        Page<className_CVO> className_LVOPage = className_LService.getclassName_CVOPage(className_LPage, request);

        return ResultUtils.success(className_LVOPage);
    }

    /**
     * 分页获取列表（仅管理员）
     *
     * @param className_LQueryRequest 查询参数
     * @param request              查询请求
     * @return 分页
     */
    @PostMapping("/list/page")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Page<className_C>> listclassName_CByPage(@RequestBody className_CQueryRequest className_LQueryRequest,
                                                           HttpServletRequest request) {
        long current = className_LQueryRequest.getCurrent();
        long size = className_LQueryRequest.getPageSize();
        Page<className_C> className_LPage = className_LService.page(new Page<>(current, size),
                className_LService.getQueryWrapper(className_LQueryRequest));
        return ResultUtils.success(className_LPage);
    }

    // endregion

}
